package mulMan.domain;

import java.io.File;
import java.io.IOException;
import java.util.Observable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

import mulMan.services.AudioFileServices;

@Entity
@Table(name = "AUDIO_FILE")
public class AudioFile extends Observable{
	
	private Integer idAudioFile;
	private String title;
	private String artist;
	private String album;
	private int length;
	private String path;
	
	
	private AudioFileServices audioFileServices;
	
	@Transient
	public AudioFileServices getAudioFileServices() {
		return audioFileServices;
	}

	public void setAudioFileServices(AudioFileServices audioFileServices) {
		this.audioFileServices = audioFileServices;
	}

	
	
	
	public AudioFile() {
		
	}
	
	//Getters and Setters
	@Id
	@GeneratedValue
	@Column(name = "ID_AUDIO_FILE")
	public Integer getIdAudioFile() {
		return idAudioFile;
	}
	public void setIdAudioFile(Integer idAudioFile) {
		this.idAudioFile = idAudioFile;
		setChanged();
		notifyObservers("id");
	}
	@Column(name = "TITLE")
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
		setChanged();
		notifyObservers("title");
	}
	@Column(name = "ARTIST")
	public String getArtist() {
		return artist;
	}
	public void setArtist(String artist) {
		this.artist = artist;
		setChanged();
		notifyObservers("artist");
	}
	@Column(name = "ALBUM")
	public String getAlbum() {
		return album;
	}
	public void setAlbum(String album) {
		this.album = album;
		setChanged();
		notifyObservers("album");
	}
	@Column(name = "LENGTH")
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
		setChanged();
		notifyObservers("length");
	}
	@Column(name = "PATH")
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
		setChanged();
		notifyObservers("path");
	}
	

	public String toString() {
		return title + " - " + artist;
	}
	@Override
	public boolean equals(Object obj) {
		boolean equals = false;
		
		if (obj instanceof AudioFile) {
			if (((AudioFile) obj).getIdAudioFile() == this.getIdAudioFile()) {
				equals = true;
			}
		}
		
		return equals;
	}
	@Override
	public int hashCode() {
		return 102233 * this.idAudioFile;
	}
	
	


	//Business methods

	public void play() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
		File soundFile = new File(getPath());
	    AudioInputStream sound;
		sound = AudioSystem.getAudioInputStream(soundFile);
		DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
		Clip clip = (Clip) AudioSystem.getLine(info);
	    clip.open(sound);
	    clip.start();
	}

	public void saveAudioFile() throws Exception {
		audioFileServices.saveAudioFile(this);
		setChanged();
		notifyObservers("saved");
	}

	public void updateAudioFile() throws Exception {
		audioFileServices.updateAudioFile(this);
		setChanged();
		notifyObservers("updated");
	}
	
	public void deleteAudioFile() throws Exception {
		audioFileServices.deleteAudioFile(this);
		setChanged();
		notifyObservers("deleted");
	}
	
	
}
